package com.apkplug.test;

import android.os.AsyncTask;

import com.apkplug.test.lib.LoginManager;

/**
 * Created by bshao on 10/29/15.
 */
public class LoginManagerImpl implements LoginManager {
    private boolean mHasLogin;

    @Override
    public boolean isLogin() {
        return mHasLogin;
    }

    @Override
    public void loginAsync(String username, String password, LoginCallback callback) {
        mockLoginSuccess(callback);
    }

    private void mockLoginSuccess(final LoginCallback callback) {
        new AsyncTask<Void, Void, Void>() {

            @Override
            protected Void doInBackground(Void... params) {
                try {
                    Thread.sleep(2000L);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                return null;
            }

            @Override
            protected void onPostExecute(Void aVoid) {
                super.onPostExecute(aVoid);
                mHasLogin = true;
                callback.onSuccess("ok");
            }
        }.execute();
    }

    private void mockLoginFail(final LoginCallback callback) {
        new AsyncTask<Void, Void, Void>() {

            @Override
            protected Void doInBackground(Void... params) {
                try {
                    Thread.sleep(2000L);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                return null;
            }

            @Override
            protected void onPostExecute(Void aVoid) {
                super.onPostExecute(aVoid);
                mHasLogin = false;
                callback.onFail(500);
            }
        }.execute();
    }
}
